package com.yipinhouse.signing.app.appImpl;

import cn.hutool.core.bean.BeanUtil;
import com.alipay.api.AlipayApiException;
import com.yipinhouse.common.result.Result;
import com.yipinhouse.signing.app.WebUtil;
import com.yipinhouse.signing.client.IntentionMoneyManageClient;
import com.yipinhouse.signing.client.param.IntentionMoneyParam;
import com.yipinhouse.signing.client.result.IntentionMoneyResult;
import com.yipinhouse.signing.service.IntentionMoneyService;
import com.yipinhouse.signing.service.dto.IntentionMoneyDto;
import com.yipinhouse.signing.service.param.IntentionMoneyServiceParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("intentionMoney")
public class IntentionMoneyManageAppImpl implements IntentionMoneyManageClient {

    @Resource
    private IntentionMoneyService intentionMoneyService;

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 新增意向金记录
     * @param intentionMoneyParam
     * @return
     */
    @Override
    public Result insertIntentionMoney(IntentionMoneyParam intentionMoneyParam) throws InterruptedException {
        log.info("新增意向金记录的入参是" + intentionMoneyParam);
        IntentionMoneyServiceParam param = BeanUtil.copyProperties(intentionMoneyParam, IntentionMoneyServiceParam.class);
        //TODO 从请求头取出userId
        IntentionMoneyDto intentionMoneyDto= intentionMoneyService.insertIntentionMoney(param);
        IntentionMoneyResult intentionMoneyResult = BeanUtil.copyProperties(intentionMoneyDto, IntentionMoneyResult.class);
        return Result.getInstance(IntentionMoneyResult.class).setMsg("成功！").setCode(200).setData(intentionMoneyResult);
    }

    @Override
    public void pay(String intentionMoneyNum) throws Exception {
        String result = intentionMoneyService.pay(intentionMoneyNum);
        if (result != null){
            Message<String> message = MessageBuilder.withPayload(intentionMoneyNum).build();
            rocketMQTemplate.syncSend("yipinhouse-check-pay-status",message,10000,4);
            WebUtil.getResponse().setCharacterEncoding("utf-8");
            WebUtil.getResponse().setContentType("text/html;charset=utf-8");
            WebUtil.getResponse().getWriter().write(result);
            WebUtil.getResponse().getWriter().flush();
        }else  WebUtil.getResponse().sendRedirect("payment-fail.html");
    }

    @Override
    public void payBack() {
        log.info("执行支付回调");
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String status = request.getParameter("trade_status");
        if (status!= null && "TRADE_SUCCESS".equals(status)) {
            String intentionMoneyId = request.getParameter("out_trade_no");
            Integer id = Integer.valueOf(intentionMoneyId);
            intentionMoneyService.payok(id);
        }
    }
}
